import Vue from "vue";
import Vuex from "vuex";
// import plugins from './plugins'
import VuexPersistence from "vuex-persist";
Vue.use(Vuex);

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context("./modules", true, /\.js$/); //内嵌所有模块

const vuexSession = new VuexPersistence({
  key: "ANYWHERE::HT",
  storage: window.sessionStorage
});

// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
  const value = modulesFiles(modulePath);
  modules[moduleName] = value.default;
  return modules;
}, {}); //模块注册

const debug = process.env.NODE_ENV !== "production";

export default new Vuex.Store({
  modules: modules,
  strict: debug,
  getters: {},
  actions: {},
  state: {},
  mutations: {},
  plugins: [vuexSession.plugin]
});
